﻿using AutoMapper;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools.Core;
using Tools.Core.Dtos;
using Tools.Core.Entities;
using Tools.Core.Extensions;
using Tools.Core.Mvvm;

namespace Tools.Modules.Navigations.ViewModels
{
    public class ImageConvertViewModel : BindableBase,INavigationAware
    {
        private ObservableCollection<string> _imageTypes=new ObservableCollection<string>();
        public ObservableCollection<string> ImageTypes
        {
            get => _imageTypes;
            set=>SetProperty(ref _imageTypes, value);
        }
        private string _targetType;
        public string TargetType
        {
            get => _targetType;
            set => SetProperty(ref _targetType, value);
        }
        private string _imageOutput;
        public string ImageOutput
        {
            get => _imageOutput;
            set=>SetProperty(ref _imageOutput, value);
        }
        private ObservableCollection<ImageConvertPlanDto> _imagePlans = new ObservableCollection<ImageConvertPlanDto>();
        public ObservableCollection<ImageConvertPlanDto> ImagePlans
        {
            get=>_imagePlans;
            set=>SetProperty(ref _imagePlans, value);
        }
        private ObservableCollection<string> _images=new ObservableCollection<string>();
        public ObservableCollection<string> Images
        {
            get => _images;
            set => SetProperty(ref _images, value);
        }

        public DelegateCommand ChooseImagesCommand { get; private set; }
        public DelegateCommand ChangeImageOutputCommand { get; private set; }
        public DelegateCommand ConvertCommand { get; private set; }
        public ImageConvertViewModel(GlobalConfig config)
        {
            ImageOutput = config.ImageOutput;
            ImageTypes = config.ImageTypes;
            ChooseImagesCommand = new DelegateCommand(() =>
            {
                var dialog = new Microsoft.Win32.OpenFileDialog();
                dialog.Multiselect = true;
                if (dialog.ShowDialog() == true) 
                {
                    Images = dialog.FileNames.AsObservableCollection();
                }
            });
            ChangeImageOutputCommand = new DelegateCommand(() =>
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    ImageOutput = fbd.SelectedPath;
                    Kits.WriteObject(config);
                }
            });
            ConvertCommand = new DelegateCommand(() =>
            {
                var addPlans = Images.Select(image => {
                    FileInfo f = new FileInfo(image);

                    return new ImageConvertPlanDto
                    {
                        FromPath = f.FullName,
                        TargetType=TargetType,
                        Name = f.Name,
                        OutputDir = ImageOutput,
                        CreateTime = DateTime.Now,
                    };
                }).AsObservableCollection();
                ImagePlans=ImagePlans.Concat(addPlans).AsObservableCollection();
                TasksQueueHelper.GetQueue("ImageConvert").Enqueue(() =>
                {
                    Softwares.ConvertImages(addPlans);
                    return Task.CompletedTask;
                });
            },() => Images.Count > 0 && TargetType != string.Empty).ObservesProperty(()=>Images).ObservesProperty(()=>TargetType);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
    }
}
